var YDVERSION = '1.0.0';
var topWindow = window;
while (topWindow.parent) {
	if (topWindow.parent == topWindow) break;
	if (topWindow.parent.YDVERSION) {
		topWindow = topWindow.parent;
	} else {
		break;
	}
}
topWindow.editCovers = topWindow.editCovers || [];
topWindow.editWindows = topWindow.editWindows || [];
function showCustomWindow(windowEle) {
	var coverZIndex = 20000 + topWindow.editCovers.length * 10, windowZIndex = 20000 + topWindow.editWindows.length * 10 + 5;
	var thisCover = $('<div class="windowMaskCommon" style="z-index:' + coverZIndex + ';"></div>');
	if (windowEle.attr('type') == 0) {
		$(topWindow.document.body).append(thisCover);
		$(topWindow.document.body).append(windowEle);
	} else {
		$(window.document.body).append(thisCover);
	}
	windowEle.show();
	windowEle.css('z-index', windowZIndex);
	var windowWidth = windowEle.attr('type') == 0 ? topWindow.document.body.offsetWidth : window.document.body.offsetWidth;
	var windowHeight = windowEle.attr('type') == 0 ? topWindow.document.body.offsetHeight : window.document.body.offsetHeight;
	windowEle.css({ left: (windowWidth - windowEle.width()) / 2, top: (windowHeight - windowEle.height()) / 2 });
	topWindow.editCovers.push(thisCover);
	topWindow.editWindows.push(windowEle);
}

function hideCustomWindow() {
	var firstCover = topWindow.editCovers.pop(), firstWindow = topWindow.editWindows.pop();
	firstCover.remove();
	if (firstWindow.attr('type') == 0) {
		firstWindow.remove();
	} else {
		firstWindow.hide();
	}
}

function showWarnDialog(warnMessage) {
	var warnHtml = '<div class="windowCommon" type="0" style="width:240px;height:140px;">'
		+ '	<div class="windowTitleCommon" style="width:236px;height:30px;">'
		+ '		<div class="windowTitleTextCommon" style="height:30px;width:208px;line-height:30px;">提示信息</div>'
		+ '		<div class="windowTitleCloseCommon" style="width:18px;height:30px;padding-top:6px;">'
		+ '			<img class="windowCloseImageCommon" src="./images/close_grey.png" onclick="hideCustomWindow()">'
		+ '		</div>'
		+ '	</div>'
		+ '	<div class="windowContentCommon" style="width:236px;height:56px;text-align:center;line-height:56px;overflow:hidden;">' + warnMessage + '</div>'
		+ '	<div style="width:236px;height:50px;padding-top:12px;text-align:right;padding-right:10px;">'
		+ '		<input class="btnCommon btnSure" type="button" value="确定" onclick="hideCustomWindow()"/>'
		+ '	</div>'
		+ '</div>';
	showCustomWindow($(warnHtml));
}

function showAutoCloseDialog(warnMessage) {
	var warnHtml = '<div class="windowCommon" type="0" style="width:240px;height:140px;">'
		+ '	<div class="windowTitleCommon" style="width:236px;height:30px;">'
		+ '		<div class="windowTitleTextCommon" style="height:30px;width:208px;line-height:30px;">提示信息</div>'
		+ '		<div class="windowTitleCloseCommon" style="width:18px;height:30px;padding-top:6px;">'
		+ '			<img class="windowCloseImageCommon" src="../images/close_grey.png" onclick="hideCustomWindow()">'
		+ '		</div>'
		+ '	</div>'
		+ '	<div class="windowContentCommon" style="width:236px;height:56px;text-align:center;line-height:56px;overflow:hidden;">' + warnMessage + '</div>'
		+ '</div>';
	showCustomWindow($(warnHtml));
	setTimeout(hideCustomWindow, 1500);
}
//首页专用（路径问题）
function showWarnDialog2(warnMessage) {
	var warnHtml = '<div class="windowCommon" type="0" style="width:240px;height:140px;">'
		+ '	<div class="windowTitleCommon" style="width:236px;height:30px;">'
		+ '		<div class="windowTitleTextCommon" style="height:30px;width:208px;line-height:30px;">提示信息</div>'
		+ '		<div class="windowTitleCloseCommon" style="width:18px;height:30px;padding-top:6px;">'
		+ '			<img class="windowCloseImageCommon" src="../images/close_grey.png" onclick="hideCustomWindow()">'
		+ '		</div>'
		+ '	</div>'
		+ '	<div class="windowContentCommon" style="width:236px;height:56px;text-align:center;line-height:56px;overflow:hidden;">' + warnMessage + '</div>'
		+ '	<div style="width:236px;height:50px;padding-top:12px;text-align:right;padding-right:10px;">'
		+ '		<input class="btnCommon btnSure" type="button" value="确定" onclick="hideCustomWindow()"/>'
		+ '	</div>'
		+ '</div>';
	showCustomWindow($(warnHtml));
}

function showConfirmDialog(confirmMessage, callback) {
	var warnHtml = '<div class="windowCommon" type="0" style="width:240px;height:140px;">'
		+ '	<div class="windowTitleCommon" style="width:236px;height:30px;">'
		+ '		<div class="windowTitleTextCommon" style="height:30px;width:208px;line-height:30px;">提示信息</div>'
		+ '		<div class="windowTitleCloseCommon" style="width:18px;height:30px;padding-top:6px;">'
		+ '			<img class="windowCloseImageCommon" src="../images/close_grey.png" onclick="hideCustomWindow()">'
		+ '		</div>'
		+ '	</div>'
		+ '	<div class="windowContentCommon" style="width:236px;height:56px;text-align:center;line-height:56px;overflow:hidden;">' + confirmMessage + '</div>'
		+ '	<div style="width:236px;height:50px;text-align:center;padding-top:12px;">'
		+ '		<input class="btnCommon btnClose" type="button" value="取消" onclick="hideCustomWindow()"/>'
		+ '		<input class="btnCommon btnSure" type="button" value="确定"/>'
		+ '	</div>'
		+ '</div>';
	var warnEle = $(warnHtml);
	showCustomWindow(warnEle);
	warnEle.find('.btnSure').click(function (e) {
		confirmDialog(callback);
	});
}

function confirmDialog(callback) {
	callback(true);
	hideCustomWindow();
}

function showFrameDialog(openURL, options, data, callback) {
	var windowTitle = options.title || '';
	var windowWidth = options.width || 400, windowHeight = options.height || 300;
	var frameUUID = getUUID();
	var pageHtml = '<div class="windowCommon" type="0" style="width:' + windowWidth + 'px;height:' + windowHeight + 'px;">'
		+ '	<div class="windowTitleCommon" style="width:' + (windowWidth - 4) + 'px;height:30px;">'
		+ '		<div class="windowTitleTextCommon" style="height:30px;width:' + (windowWidth - 32) + 'px;line-height:30px;">' + windowTitle + '</div>'
		+ '		<div class="windowTitleCloseCommon" style="width:18px;height:30px;padding-top:6px;">'
		+ '			<img class="windowCloseImageCommon" src="../images/close_grey.png" onclick="hideCustomWindow()">'
		+ '		</div>'
		+ '	</div>'
		+ '	<iframe name="' + frameUUID + '" src="' + openURL + '" frameborder="0" style="width:' + (windowWidth - 4) + 'px;height:' + (windowHeight - 34) + 'px;"></iframe>'
		+ '</div>';
	showCustomWindow($(pageHtml));
	topWindow.frames[frameUUID].addEventListener('message', function (event) {
		topWindow.frames[frameUUID].postData = event.data;
		topWindow.frames[frameUUID].postData = event.data;
	});
	topWindow.frames[frameUUID].postMessage(data);
	topWindow.frames[frameUUID].getPageParameterMap = function () {
		return topWindow.frames[frameUUID].postData;
	}
	topWindow.frames[frameUUID].closeDialog = function (param) {
		hideCustomWindow();
		if (param && callback) callback(param);
	};
}

function getUUID() {
	return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
		var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
		return v.toString(16);
	});
}

function checkEmpty(checkVal) {
	if (!checkVal || checkVal == null || checkVal == 'null' || checkVal == '' || checkVal == 'undefined') return true;
	return false;
}

function checkEmptyMap(obj) {
	for (var item in obj) {
		return false;
	}
	return true;
}

function mergeMapByKey(mapList) {
	var resultMap = {};
	for (var i = 0; i < mapList.length; i++) {
		var thisMap = mapList[i];
		for (var key in thisMap) {
			resultMap[key] = thisMap[key];
		}
	}
	return resultMap;
}

function delMapByKey(map, delKey) {
	var returnMap = {};
	for (var key in map) {
		if (key != delKey) {
			returnMap[key] = map[key];
		}
	}
	return returnMap;
}

function delMapByKeys(map, delKeys) {
	var returnMap = {};
	for (var key in map) {
		if (delKeys.indexOf(key) == -1) {
			returnMap[key] = map[key];
		}
	}
	return returnMap;
}

function delStringKey(delString, delKey, spliter) {
	var result = "";
	var strArr = delString.split(spliter);
	for (var i = 0; i < strArr.length; i++) {
		if (strArr[i] == delKey) continue;
		result += (result == '' ? '' : spliter) + strArr[i];
	}
	return result;
}

function mergeArray(mainArr, mergeArr) {
	for (var i = 0; i < mergeArr.length; i++) {
		if (mainArr.indexOf(mergeArr[i]) == -1) mainArr.push(mergeArr[i]);
	}
	return mainArr;
}

function deleteArrayKeys(mainArr, removeArr) {
	var resultArr = [];
	for (var i = 0; i < mainArr.length; i++) {
		if (removeArr.indexOf(mainArr[i]) == -1) resultArr.push(mainArr[i]);
	}
	return resultArr;
}

function concatListToString(dataList, concatChar) {
	var concatResult = "";
	for (var i = 0; i < dataList.length; i++) {
		concatResult += (concatResult == "" ? "" : concatChar) + dataList[i];
	}
	return concatResult;
}

//格式化xml代码
function formateXml(xmlStr) {
	text = xmlStr;
	//使用replace去空格
	text = '\n' + text.replace(/(<\w+)(\s.*?>)/g, function ($0, name, props) {
		return name + ' ' + props.replace(/\s+(\w+=)/g, " $1");
	}).replace(/>\s*?</g, ">\n<");
	//处理注释
	text = text.replace(/\n/g, '\r').replace(/<!--(.+?)-->/g, function ($0, text) {
		return '<!--' + escape(text) + '-->';
	}).replace(/\r/g, '\n');
	//调整格式	以压栈方式递归调整缩进
	var rgx = /\n(<(([^\?]).+?)(?:\s|\s*?>|\s*?(\/)>)(?:.*?(?:(?:(\/)>)|(?:<(\/)\2>)))?)/mg;
	var nodeStack = [];
	var output = text.replace(rgx, function ($0, all, name, isBegin, isCloseFull1, isCloseFull2, isFull1, isFull2) {
		var isClosed = (isCloseFull1 == '/') || (isCloseFull2 == '/') || (isFull1 == '/') || (isFull2 == '/');
		var prefix = '';
		if (isBegin == '!') {//!开头
			prefix = setXMLPrefix(nodeStack.length);
		} else {
			if (isBegin != '/') {///开头
				prefix = setXMLPrefix(nodeStack.length);
				if (!isClosed) {//非关闭标签
					nodeStack.push(name);
				}
			} else {
				nodeStack.pop();//弹栈
				prefix = setXMLPrefix(nodeStack.length);
			}
		}
		var ret = '\n' + prefix + all;
		return ret;
	});
	var outputText = output.substring(1);
	//还原注释内容
	outputText = outputText.replace(/\n/g, '\r').replace(/(\s*)<!--(.+?)-->/g, function ($0, prefix, text) {
		if (prefix.charAt(0) == '\r') prefix = prefix.substring(1);
		text = unescape(text).replace(/\r/g, '\n');
		var ret = '\n' + prefix + '<!--' + text.replace(/^\s*/mg, prefix) + '-->';
		return ret;
	});
	outputText = outputText.replace(/\s+$/g, '').replace(/\r/g, '\r\n');
	return outputText;
}

//计算头函数	用来缩进
function setXMLPrefix(prefixIndex) {
	var result = '';
	var span = '    ';//缩进长度
	var output = [];
	for (var i = 0; i < prefixIndex; ++i) {
		output.push(span);
	}
	result = output.join('');
	return result;
}